package edu.mse.camel.client.whiteboard.gef.ui.commands;

import org.eclipse.gef.commands.Command;

import edu.mse.camel.client.whiteboard.gef.whiteboard.Connection;

/**
 * A command to disconnect (remove) a connection from its endpoints.
 * The command can be undone or redone.
 * @author Elias Volanakis
 */
public class ConnectionDeleteCommand extends AbstractWhiteboardCommand {
	
	/** Connection instance to disconnect. */
	private final Connection connection;
	
	/** 
	 * Create a command that will disconnect a connection from its endpoints.
	 * @param conn the connection instance to disconnect (non-null)
	 * @throws IllegalArgumentException if conn is null
	 */ 
	public ConnectionDeleteCommand(Connection conn) {
		if (conn == null) {
			throw new IllegalArgumentException();
		}
		setLabel("connection deletion");
		this.connection = conn;
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		connection.getSource().getSourceConnections().remove(connection);
		connection.getTarget().getTargetConnections().remove(connection);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		connection.getSource().getSourceConnections().add(connection);
		connection.getTarget().getTargetConnections().add(connection);
	}
}
